package com.qinyh.model;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import com.qinyh.activity.R;
import com.qinyh.entity.Book;
import com.qinyh.entity.BookCatalog;
import com.qinyh.entity.Shelf;
import com.qinyh.util.umdutil.UMD;
import com.qinyh.util.umdutil.UMDDecoder;

/**
 * 获取数据源
 * 
 * @author Administrator
 * 
 */
public class DataObtain {

	private Context ctt;

	public Context getCtt() {
		return ctt;
	}

	public void setCtt(Context ctt) {
		this.ctt = ctt;
	}

	/**
	 * 获取书架数据
	 * 
	 * @throws Exception
	 * **/
	public Shelf getShelf() throws Exception {
		Shelf shelf = new Shelf();
		AssetManager am = ctt.getAssets();
		// 初始书Y
		InputStream in = am.open("y.umd");
		UMDDecoder ud = new UMDDecoder();
		UMD umd = ud.decode(in);
		umd.setId(1);
		shelf.setBackgroudId(R.drawable.shelf_panel);
		BookCatalog bc = new BookCatalog();
		bc.setId(R.drawable.shelf_panel);
		List<Book> lb = new ArrayList<Book>();
		if (umd != null) {
			lb.add(umd);
			Log.v("debug", "get the umd file");
		}
		// sj
		InputStream sjin = am.open("sj.umd");
		UMD sjumd = ud.decode(sjin);
		sjumd.setId(2);
		shelf.setBackgroudId(R.drawable.shelf_panel);
		BookCatalog sjbc = new BookCatalog();
		sjbc.setId(R.drawable.shelf_panel);
		List<Book> sjlb = new ArrayList<Book>();
		if (sjumd != null) {
			sjlb.add(sjumd);
			Log.v("debug", "get the umd file");
		}
		InputStream xbn = am.open("x.umd");	
		UMD x = ud.decode(xbn);
		x.setId(3);
		if (x != null)
			sjlb.add(x);
		List<BookCatalog> lbc = new ArrayList<BookCatalog>();
		bc.setCatalogName("圣经");
		bc.setBooks(lb);

		sjbc.setCatalogName("创世纪");
		sjbc.setBooks(sjlb);

		lbc.add(sjbc);
		lbc.add(bc);
		shelf.setCatalog(lbc);
		return shelf;
	}

	/**
	 * 根据ID获取指定的书
	 * */
	public Book getBookById(String id) {
		return new Book();
	}

}
